package bb.lanxing.calc.processer;

import bb.lanxing.calc.calculator.ElevationGradeCalc;
import bb.lanxing.calc.fixer.AltitudeFixer;
import bb.lanxing.calc.fixer.SpeedFixer;
import bb.lanxing.model.database.Trackpoint;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.Enums;

public class BiciGpsDataProcesser {
    private ElevationGradeCalc elevationGradeCalc = new ElevationGradeCalc();
    private AltitudeFixer altitudeFixer = new AltitudeFixer();
    private SpeedFixer speedFixer = new SpeedFixer();
    private long mLastWheelNum = 0;
    private long mLastTimestamp = 0;
    private long mTotalDistance = 0;
    private int mMaxCadence = 0;
    private int mUpDistance = 0;
    private int mFlatDistance = 0;
    private int mDownDistance = 0;
    private int mUpDuration = 0;
    private int mFlatDuration = 0;
    private int mDownDuration = 0;
    private int mElevationGain = 0;
    private int mElevationLoss = 0;
    private int mMaxGrade = 0;
    private int mMinGrade = 0;

    public void process(Trackpoint trackpoint) {
        int cadence = trackpoint.getCadence();
        if (cadence > this.mMaxCadence) {
            this.mMaxCadence = cadence;
        }
        if (this.mLastWheelNum == 0 || this.mLastTimestamp == 0) {
            this.mLastWheelNum = trackpoint.getSumWheel();
            this.mLastTimestamp = trackpoint.getTime();
            return;
        }
        long sumWheel = trackpoint.getSumWheel();
        long time = trackpoint.getTime();
        double d = ((float) (sumWheel - this.mLastWheelNum)) * 1.593f;
        long round = Math.round((time - this.mLastTimestamp) / 1000.0d);
        if (round >= 1) {
            int i = (round > 10L ? 1 : (round == 10L ? 0 : -1));
        }
        this.mLastWheelNum = sumWheel;
        this.mLastTimestamp = time;
        if (CommonUtil.isPointValid(trackpoint)) {
            this.mTotalDistance = (long) (this.mTotalDistance + d);
            float elevation = this.elevationGradeCalc.getElevation();
            float grade = this.elevationGradeCalc.getGrade();
            if (this.mTotalDistance <= 50) {
                elevation = 0.0f;
                grade = 0.0f;
            }
            if (elevation > 0.0f) {
                this.mElevationGain = (int) (this.mElevationGain + elevation);
            } else if (elevation < 0.0f) {
                this.mElevationLoss = (int) (this.mElevationLoss + elevation);
            }
            if (grade > this.mMaxGrade) {
                this.mMaxGrade = (int) grade;
            } else if (grade < this.mMinGrade) {
                this.mMinGrade = (int) grade;
            }
        }
    }

    private Enums.RoadType getRoadType(int grade) {
        Enums.RoadType roadType = Enums.RoadType.Flat;
        if (grade > 1) {
            return Enums.RoadType.Up;
        }
        return grade < -1 ? Enums.RoadType.Down : roadType;
    }

    public int getMaxCadence() {
        return this.mMaxCadence;
    }

    public int getAvgCadence(long workoutId) {
        long cadenceSumByWorkout = Trackpoint.getCadenceSumByWorkout(workoutId);
        long cadenceCountByWorkout = Trackpoint.getCadenceCountByWorkout(workoutId);
        if (cadenceCountByWorkout > 0) {
            return (int) (cadenceSumByWorkout / cadenceCountByWorkout);
        }
        return 0;
    }

    public int getUpDistance() {
        return this.mUpDistance;
    }

    public int getFlatDistance() {
        return this.mFlatDistance;
    }

    public int getDownDistance() {
        return this.mDownDistance;
    }

    public int getUpDuration() {
        return this.mUpDuration;
    }

    public int getFlatDuration() {
        return this.mFlatDuration;
    }

    public int getDownDuration() {
        return this.mDownDuration;
    }

    public int getElevationGain() {
        return this.mElevationGain;
    }

    public int getElevationLoss() {
        return this.mElevationLoss;
    }

    public int getMaxGrade() {
        return this.mMaxGrade;
    }

    public int getMinGrade() {
        return this.mMinGrade;
    }

    public void reset() {
        this.mLastWheelNum = 0L;
        this.mLastTimestamp = 0L;
        this.mTotalDistance = 0L;
        this.mMaxCadence = 0;
        this.mUpDistance = 0;
        this.mFlatDistance = 0;
        this.mDownDistance = 0;
        this.mUpDuration = 0;
        this.mFlatDuration = 0;
        this.mDownDuration = 0;
        this.mElevationGain = 0;
        this.mElevationLoss = 0;
        this.mMaxGrade = 0;
        this.mMinGrade = 0;
        this.elevationGradeCalc.reset();
        this.altitudeFixer.reset();
        this.speedFixer.reset();
    }
}
